﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;
using System.Linq;

using ProjectBase.Core;

using Survey.Common;
using Survey.Common.Validation;
using Survey.Common.Data;
using Survey.Entity;

namespace Survey.Business
{
    public class QuestionFilterValidator : FilterValidatorBase<QuestionFilter, int>
    {
        protected override void Init(RuleCollection<QuestionFilter> rules) { base.Init(rules); }
    }
    public class QuestionValidator : EntityValidatorBase<Question, int, IQuestionDao>
    {
        private static readonly DateTime LowerValidUntilBound = new DateTime(2000, 1, 1);
        private static readonly DateTime UpperValidUntilBound = new DateTime(2020, 1, 1);

        protected override void Init(RuleCollection<Question> rules)
        {
            // string
            rules.For(entity => entity.Description).RequireLengthBetween(0, 2000);

            // custom
            rules.Assure(CheckAnswersValidity);

            // value type
            rules.For(entity => entity.ValidUntil)
                .RequireValueBetween(LowerValidUntilBound, UpperValidUntilBound)
                .BreakOnError();
            
            // unique Code
            rules.For(entity => entity.Title).RequireLengthBetween(5, 250)
                .BreakOnError();
            rules.IsUnique(entity => Dao.Find(entity.ID, new QuestionFilter
                {
                    TitleExact = entity.Title,
                    ValidUntil = entity.ValidUntil,
                }));
        }

        protected ValidationResult CheckAnswersValidity(Question item, out ValidationMessage message)
        {
            message = null;
             //if (!this.CheckIsNotEmpty(() => entity.Answers))
             //{
             //    return ValidationResult.Error;
             //}

             //var result = true;
             //foreach (var answer in entity.Answers)
             //{
             //    result &= this.CheckLength(() => answer.Title, 1, 250);
             //    result &= this.CheckLength(() => answer.Description, 0, 2000);
             //}
             //return result;
            return ValidationResult.IsValid;
        }
    }
}